<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="../js/mui.min.js"></script>
		<link href="../css/mui.min.css" rel="stylesheet" />
<script src="../js/Common.js"></script>
		<style type="text/css">
			.mui-grid-9 {
				background: white !important;
			}
			
			li {
				border-left: none !important;
			}
			
			img {
				width: 50px !important;
			}
			
			body {
				background: transparent !important;
			}
			
			.mui-content {
				background: white;
				text-align: center;
			}
			
			.mui-grid-view.mui-grid-9 .mui-table-view-cell {
    margin: 0;
    padding: 11px 15px;
    vertical-align: top;
     border-right: 0px solid #eee; 
     border-bottom: 0px solid #eee; 
}
			
			
		</style>
	</head>

	<body onclick="closeShare();">
		<div class="mui-content">
			<img id="pic" src="img/add.png" style="display: none;" />
		</div>
		<footer style="position: absolute;bottom: 0px; width:100%;background: white;">
			<div style="height: 30px; line-height: 30px;font-weight: bold;font-family: '微软雅黑'; margin-left: 10px;">分享到</div>
			<ul class="mui-table-view mui-grid-view mui-grid-9">
				<li onclick="shareHref(0);" class="mui-table-view-cell mui-media mui-col-xs-4">
					<img class="mui-media-object" src="../images/share/share_to_icon_wx.png">
					<div class="mui-media-body">微信</div>
				</li>
				
				<li onclick="shareHref(1);" class="mui-table-view-cell mui-media mui-col-xs-4">
					<img class="mui-media-object" src="../images/share/share_to_icon_wxq.png">
					<div class="mui-media-body">朋友圈</div>
				</li>

				<li onclick="copyToClip('2');" class="mui-table-view-cell mui-media mui-col-xs-4">
					<img class="mui-media-object" src="../images/share/share_to_icon_copy.png">
					<div class="mui-media-body">复制粘贴</div>
				</li>
			</ul>
		</footer>
		<!--<input id="sharehref" style="display: none;" class="sharehref" type="url" value="http://www.99990158.com/" placeholder="请输入要分享的链接地址" />
		<input id="sharehrefTitle" style="display: none;" class="sharehref" type="text" value="中诚黄金" placeholder="请输入要分享的链接标题" />
		<input id="sharehrefDes" style="display: none;" class="sharehref" type="text" value="中诚黄金" placeholder="请输入要分享的链接描述" />-->
	</body>

	<script type="text/javascript">
		function SetShareInfo(url, title, des) {
			document.getElementById('sharehref').value = url;
			document.getElementById('sharehrefTitle').value = title;
			document.getElementById('sharehrefDes').value = des;
		}
		var shares = null,
			bhref = false;
		var Intent = null,
			File = null,
			Uri = null,
			main = null;
		//来源页面
		var pageSourceId;
		var shareInfo;
		mui.init();
		mui.plusReady(function() {
			updateSerivces();
			if(plus.os.name == "Android") {
				Intent = plus.android.importClass("android.content.Intent");
				File = plus.android.importClass("java.io.File");
				Uri = plus.android.importClass("android.net.Uri");
				main = plus.android.runtimeMainActivity();
			}
			shareInfo = plus.webview.currentWebview().shareInfo;

			/*console.log(JSON.stringify(shareInfo));
			sharehref.value = shareInfo.href;
			sharehrefTitle.value = shareInfo.title;
			sharehrefDes.value = shareInfo.content;
			console.log("pageSource:" + pageSourceId);*/

			pageSourceId = shareInfo.pageSourceId;

		});
		/**
		 * 更新分享服务
		 */
		function updateSerivces() {
			plus.share.getServices(function(s) {
				shares = {};
				for(var i in s) {
					var t = s[i];
					shares[t.id] = t;
				}
			}, function(e) {
				outSet("获取分享服务列表失败：" + e.message);
			});
		}
		/**
		 * 调用系统分享
		 * 调用
		 */
		function shareSystem() {
			if(plus.os.name !== "Android") {
				plus.nativeUI.alert("此平台暂不支持系统分享功能!");
				return;
			}
			var intent = new Intent(Intent.ACTION_SEND);
			var p = "";
			if(pic && pic.realUrl) {
				p = pic.realUrl;
				if(p.substr(0, 7) === "file://") {
					p = p.substr(7);
				} else if(p.sub(0) !== "/") {
					p = plus.io.convertLocalFileSystemURL(p);
				}
			}
			var f = new File(p);
			var uri = Uri.fromFile(f);
			if(f.exists() && f.isFile()) {
				console.log("image/*");
				intent.setType("image/*");
				intent.putExtra(Intent.EXTRA_STREAM, uri);
			} else {
				console.log("text/plain");
				intent.setType("text/plain");
			}
			intent.putExtra(Intent.EXTRA_SUBJECT, "中诚黄金");
			intent.putExtra(Intent.EXTRA_TEXT, sharehrefDes.value + "-" + sharehref.value);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			main.startActivity(Intent.createChooser(intent, "系统分享"));
		}
		/**
		 * 分享操作
		 * @param {String} id
		 */
		function shareAction(id, ex) {
			console.log(id);
			console.log(ex);
			var s = null;
			console.log("分享操作：");
			if(!id || !(s = shares[id])) {
				console.log("无效的分享服务！");
				return;
			}
			if(s.authenticated) {
				console.log("---已授权---");
				shareMessage(s, ex);
			} else {
				console.log("---未授权---");
				s.authorize(function() {
					shareMessage(s, ex);
				}, function(e) {
					console.log("认证授权失败：" + e.code + " - " + e.message);
				});
			}
		}
		/**
		 * 发送分享消息
		 * @param {plus.share.ShareService} s
		 */
		function shareMessage(s, ex) {
			var appmsg = {
				"href": "http://a.app.qq.com/o/simple.jsp?pkgname=com.zc.goldApp",
				"title": "中诚黄金",
				"content": "中诚黄金是上海黄金交易所的会员，专业从事黄金T+D业务的咨询服务，致力于为广大客户提供及时、全面、专业的黄金投资咨询服务。",
				"thumbs": ["_www/logo.png"]
			};
			var msg = {
				content: shareInfo.content,
				extra: {
					scene: ex
				}
			};
			console.log("bhref:" + bhref);

			bhref = true;
			if(bhref) {
				msg.href = shareInfo.href ? shareInfo.href : appmsg.href;
				msg.title = shareInfo.title ? shareInfo.title : appmsg.title;
				msg.content = shareInfo.content ? shareInfo.content : appmsg.content;
				msg.thumbs = shareInfo.thumbs ? shareInfo.thumbs : appmsg.thumbs;
			} else {
				if(pic && pic.realUrl) {
					msg.pictures = [pic.realUrl];
				}
			}
			console.log(JSON.stringify(msg));
			s.send(msg, function() {
				console.log("分享到\"" + s.description + "\"成功！ ");
			}, function(e) {
				console.log("分享到\"" + s.description + "\"失败: " + e.code + " - " + e.message);
			});
		}
		// 打开分享 
		function shareShow() {
			bhref = false;
			var ids = [{
					id: "weixin",
					ex: "WXSceneSession"
				}, {
					id: "weixin",
					ex: "WXSceneTimeline"
				}, {
					id: "sinaweibo"
				}, {
					id: "tencentweibo"
				}],
				bts = [{
					title: "发送给微信好友"
				}, {
					title: "分享到微信朋友圈"
				}, {
					title: "分享到新浪微博"
				}, {
					title: "分享到腾讯微博"
				}];
			if(plus.os.name == "iOS") {
				ids.push({
					id: "qq"
				});
				bts.push({
					title: "分享到QQ"
				});
			}
			plus.nativeUI.actionSheet({
					cancel: "取消",
					buttons: bts
				},
				function(e) {
					var i = e.index;
					if(i > 0) {
						shareAction(ids[i - 1].id, ids[i - 1].ex);
					}
				}
			);
		}
		// 分析链接 
		function shareHref(index) {
			bhref = true;
			var ids = [{
					id: "weixin",
					ex: "WXSceneSession"
				}, {
					id: "weixin",
					ex: "WXSceneTimeline"
				}],
				bts = [{
					title: "发送给微信好友"
				}, {
					title: "分享到微信朋友圈"
				}];
			if(plus.os.name == "iOS") {
				ids.push({
					id: "qq"
				});
				bts.push({
					title: "分享到QQ"
				});
			}

			shareAction(ids[index].id, ids[index].ex);
		}
		mui.back = function() {
			var sourcePage = plus.webview.getWebviewById(pageSourceId);
			if(sourcePage) {
				sourcePage.evalJS("closeMask()");
			}
		}

		function closeShare() {
			console.log("e:" + event.target.name);
		}

		function copyToClip(text) {
			var content = shareInfo.title +  " "+ shareInfo.content + " ,请点击："+ shareInfo.href ;

			var _p = plus.os.name === 'Android' ? 'a' : plus.os.name === 'iOS' ? 'i' : '';
			if(_p != 'a') {
				var UIPasteboard = plus.ios.importClass("UIPasteboard");
				var generalPasteboard = UIPasteboard.generalPasteboard();
				// 设置/获取文本内容:
				generalPasteboard.setValueforPasteboardType(content, "public.utf8-plain-text");
				var value = generalPasteboard.valueForPasteboardType("public.utf8-plain-text");
			} else {
				var Context = plus.android.importClass("android.content.Context");
				var main = plus.android.runtimeMainActivity();
				var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
				plus.android.invoke(clip, "setText", content);
			}
			outSet("复制成功，请粘贴分享。")

		}
	</script>

</html>